#!/bin/bash
# 构建 Flatpak 包

set -e

# 配置参数
APP_ID="org.mirror-tool"
MANIFEST="flatpak/org.mirror-tool.yml"
OUTPUT_DIR="../build/flatpak"
RUNTIME="org.freedesktop.Platform"
RUNTIME_VERSION="22.08"

# 安装依赖
if ! command -v flatpak &> /dev/null; then
    echo "安装 Flatpak..."
    sudo apt install -y flatpak flatpak-builder
    flatpak remote-add --if-not-exists flathub https://flathub.org/repo/flathub.flatpakrepo
fi

# 清理旧构建
rm -rf build-dir

# 构建应用
flatpak-builder --repo=MirrorToolRepo build-dir "$MANIFEST" \
    --force-clean \
    --install-deps-from=flathub \
    --runtime="$RUNTIME" \
    --runtime-version="$RUNTIME_VERSION"

# 生成单文件包
mkdir -p "$OUTPUT_DIR"
flatpak build-bundle MirrorToolRepo "$OUTPUT_DIR/$APP_ID.flatpak" "$APP_ID"

echo "构建成功: $OUTPUT_DIR/$APP_ID.flatpak"